package data;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import obj_file.Question;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Question_File {
	
	int id = 0 , DapAnDung = 0;
	String NoiDung,DapAnA , DapAnB ,DapAnC , DapAnD ;
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder  dBuilder;
	Document doc  ;	

	public Question_File()
	{}
	
	public void ReadFile() throws ParserConfigurationException, SAXException, IOException
	{
		  dbFactory = DocumentBuilderFactory.newInstance();
		  dBuilder = dbFactory.newDocumentBuilder();
		  doc = dBuilder.parse("Question.xml");
	}
	public void InsertQuestion(int id , String NoiDung , String DapAnA , String DapAnB , String DapAnC ,String DapAnD , int DapAnDung) throws ParserConfigurationException, SAXException, IOException, TransformerException
	{		
		  ReadFile();
		  Node rootNode = doc.getElementsByTagName("Menu").item(0);
		  
	// Add Tab "CauHoi"
		  Element cauhoi = doc.createElement("CauHoi");
		  cauhoi.setAttribute("id", Integer.toString(id));
		  rootNode.appendChild(cauhoi);
		  
	// Add Tab "NoiDung"
		  Element noidung = doc.createElement("NoiDung");
		  noidung.appendChild(doc.createTextNode(NoiDung));
		  cauhoi.appendChild(noidung);
	
	// Add Tab "DapAnA"
		  Element dapanA = doc.createElement("DapAnA");
		  dapanA.appendChild(doc.createTextNode(DapAnA));
		  cauhoi.appendChild(dapanA);
		
	// Add Tab "DapAnB"
		  Element dapanB = doc.createElement("DapAnB");
		  dapanB.appendChild(doc.createTextNode(DapAnB));
		  cauhoi.appendChild(dapanB);
		  
	// Add Tab "DapAnC"
		  Element dapanC = doc.createElement("DapAnC");
		  dapanC.appendChild(doc.createTextNode(DapAnC));
		  cauhoi.appendChild(dapanC);

	// Add Tab "DapAnD"
		  Element dapanD = doc.createElement("DapAnD");
		  dapanD.appendChild(doc.createTextNode(DapAnD));
		  cauhoi.appendChild(dapanD);
		  
	// Add Tab "DapAnDung"
		  Element dapandung = doc.createElement("DapAnDung");
		  dapandung.appendChild(doc.createTextNode(Integer.toString(DapAnDung)));
		  cauhoi.appendChild(dapandung);
		  
	//write the content into xml file
		  TransformerFactory transformerFactory = TransformerFactory.newInstance();
		  Transformer transformer = transformerFactory.newTransformer();
		  transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // New Line
		  DOMSource source = new DOMSource(doc);
		  StreamResult result = new StreamResult(new File("Question.xml"));
		  transformer.transform(source, result);

		  System.out.println("Done");
	}
	public Question[] ChooseQuestion() throws ParserConfigurationException, SAXException, IOException
	{		
		ReadFile();
		System.out.println(doc.getDocumentElement().getNodeName());
		NodeList nList = doc.getElementsByTagName("CauHoi");
		Question[] QList = new Question[nList.getLength()];
		for(int temp = 0 ; temp < nList.getLength() ; temp++)
		{
			Node nNode  = nList.item(temp);
			Element eElement = (Element) nNode ;
			
			id = Integer.parseInt(eElement.getAttribute("id"));
			DapAnDung = Integer.parseInt(eElement.getElementsByTagName("DapAnDung").item(0).getTextContent());
			NoiDung= eElement.getElementsByTagName("NoiDung").item(0).getTextContent();
			DapAnA = eElement.getElementsByTagName("DapAnA").item(0).getTextContent();
			DapAnB = eElement.getElementsByTagName("DapAnB").item(0).getTextContent();
			DapAnC = eElement.getElementsByTagName("DapAnC").item(0).getTextContent();
			DapAnD = eElement.getElementsByTagName("DapAnD").item(0).getTextContent();
			
			QList[temp] = new Question();
			QList[temp].Make_Question(id, NoiDung, DapAnA, DapAnB, DapAnC, DapAnD, DapAnDung);
		}
		
		Random rand = new Random() ;
		int ID_Question18 , i =0;
		Question[] Question18 = new Question[18];
		Vector v = new Vector();
		
		while(i<18)
		{
				ID_Question18 = rand.nextInt(QList.length);
				Question18[i] = new Question();
				Question18[i] = QList[ID_Question18];
				if(!v.contains(Question18[i]))
				{
					v.add(Question18[i]);
					i++;
				}
		}
		return Question18;
			
	}
}
